{{ $_ := set . "Env" (.Env | default "FE") }}

image: {{ .ModuleName }}/{{ .ImageName }}
fromImage: common/nginx-static
import:
- artifact: {{ .ModuleName }}/{{ .ImageName }}/static
  add: /app/_site/documentation
  to: /app/platform
  before: setup
  excludePaths:
  - '*/gs/'
  - '*/guides/'
  - search-index.json
  - '*/sitemap*.xml'
  - '*/robots.txt'
docker:
  ENTRYPOINT: [ "/opt/nginx-static/sbin/nginx", "-g", "daemon off;" ]
---
artifact: {{ .ModuleName }}/{{ .ImageName }}/static
from: {{ .Images.BASE_JEKYLL }}
ansible:
  install:
    - shell: |
        apk add rsync jq
        gem update --system 3.2.3
        gem install bundler -v 2.4.3
    - name: "Install Dependencies"
      shell: bundle install
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation/
  setup:
    - name: "Create modules.yml"
      shell: |
        export MODULES_DIR=/comparison
        bash ./modules_list.sh 1>_data/modules.yaml
        cat modules_menu_skip  | cut -d\- -f 2-  | jq -nRc '[inputs] | map({"name":.}) | {module_names:.}' > _data/exclude.json
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation/
    - name: "{{ .Env }} version. Merging modules of different releases..."
      shell: |
        cp -rf /src/be/modules /src >& /dev/null
        cp -rf /src/se/modules /src >& /dev/null
        cp -rf /src/ee/modules /src >& /dev/null
        cp -rf /src/fe/modules /src >& /dev/null
        cp /src/modules/010-prometheus-crd/crds/*.yaml /src/modules/300-prometheus/crds
        rm -rf /src/modules/010-prometheus-crd
      args:
        executable: /bin/bash
    - name: "Preparing modules structure"
      shell: |
        export MODULES_SRC_DIR=/src/modules
        export MODULES_DST_EN=/srv/jekyll-data/documentation/modules_en
        export MODULES_DST_RU=/srv/jekyll-data/documentation/modules_ru
        bash ./modules_prepare.sh
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation/
    - name: "Adding permalinks..."
      shell: |
        for i in $(find . -regex '.*.md' -print); do
          grep -q "^---" $i
          if [ $? -gt 0 ]; then continue; fi
          cat $i | tr -d '\n' | grep -lv "^---.*permalink: .*---" &> /dev/null
          if [ $? -eq 0 ]; then
            # permalink is absent, add permalink
            # PERMALINK="/$(echo $i | sed -E 's#(modules_)(en|ru)/#\2/modules/#' | sed 's#docs/##g'| tr '[:upper:]' '[:lower:]' | sed 's#\.md$#.html#' | sed 's#^\.\/##' | sed 's#readme\.html$#index.html#' )"
            PERMALINK="/$(echo $i | sed -E 's#(modules_)(en|ru)/#\2/modules/#' | sed 's#docs/##g'| tr '[:upper:]' '[:lower:]' | sed 's#\.md$#.html#' | sed 's#^\.\/##' | sed 's#readme\.html$##' )"
            sed -i "1apermalink: $PERMALINK" $i
          fi
        done
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation/
    - name: "Generating configuration schemas..."
      shell: |
        export MODULES_DIR=modules_en
        bash ./modules_generate_configuration.sh
        export MODULES_DIR=modules_ru MODULES_LANG=ru
        bash ./modules_generate_configuration.sh
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation
    - name: "Generating cluster configuration schemas..."
      shell: |
        export MODULES_DIR=modules_en
        bash ./generate_cluster_configuration.sh
        export MODULES_DIR=modules_ru MODULES_LANG=ru
        bash ./generate_cluster_configuration.sh
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation
    - name: "Generating CR schemas..."
      shell: |
        export MODULES_DIR=modules_en
        bash ./modules_generate_cr.sh
        export MODULES_DIR=modules_ru
        bash ./modules_generate_cr.sh
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation
    - name: "Preparing OSS licence info..."
      shell: |
        export MODULES_DIR=/src/modules
        bash ./modules_generate_ossinfo.sh
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation/
    - name: "Extracting the default Kubernetes version..."
      shell: |
        echo "default: \"$(grep "DefaultKubernetesVersion" -m 1 _data/dhctl-base.go | grep -Eo '[0-9.]+')\"" > _data/version_kubernetes.yml
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation/
    - name: "Filling in the array of supported OS & K8S versions..."
      shell: |
        sed '/^bashible:/r version_map_addition.yml' version_map.yml >> supported_versions.yml
      args:
        warn: false
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation/_data/
    - name: "Convert editions structure"
      shell: |
        /usr/local/bin/yq e -j editions-source.yaml | jq -M 'reduce .editions[] as $item ({}; .[$item.name | ascii_downcase] = $item)' > editions.json
      args:
        warn: false
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation/_data/
    - name: "Converting public documentation links to internal relative..."
      shell: |
        grep -rl '](https://deckhouse.io' /srv/jekyll-data/documentation/_data/schemas/ | \
          xargs sed -Ei 's$\]\(https://deckhouse.io/(ru|en)/documentation/v[0-9]+/(modules/|#)$](/\1/\2$'
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation/
    - copy:
        content: |
          {{- .Files.Get (printf "%smodules/810-documentation/images/web/_config.yml" .ModulePath) | nindent 10 }}
        dest: /tmp/_config_additional.yml
    - copy:
        content: |
          {{- .Files.Get (printf "%smodules/810-documentation/images/web/site/_data/topnav.yml" .ModulePath) | nindent 10 }}
        dest: /srv/jekyll-data/documentation/_data/topnav.yml
    - copy:
        content: |
          {{- .Files.Get (printf "%smodules/810-documentation/images/web/site/_data/topnav.yml" .ModulePath) | nindent 10 }}
        dest: /srv/jekyll-data/site/_data/topnav.yml
    - copy:
        content: |
          {{- .Files.Get (printf "%smodules/810-documentation/images/web/site/_includes/footer.html" .ModulePath) | nindent 10 }}
        dest: /srv/jekyll-data/site/_includes/footer.html
    - name: "Creating additional config..."
      shell: |
        echo -e "\nd8Revision: {{ .Env }}" >> /tmp/_config_additional.yml
      args:
        executable: /bin/bash
    - name: "Generating static files of the documentation part..."
      shell: |
        mkdir -m 777 -p /app/_site/
        JEKYLL_ENV=production jekyll build -d /app/_site/documentation/ --config _config.yml,/tmp/_config_additional.yml
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation/
        warn: false
    - name: "Get Deckhouse CLI version"
      shell: |
        grep -E '^\{\{- \$version' /src/registrypackages/werf.inc.yaml | sed -E 's/.+\"(.+)\".+/version: "\1"/' >> deckhouse-cli.yaml
        echo "Deckhouse CLI version: $(cat deckhouse-cli.yaml)"
      args:
        warn: false
        executable: /bin/bash
        chdir: /srv/jekyll-data/documentation/_data/
    - name: "Generating static file's of the main part..."
      shell: |
        JEKYLL_ENV=production jekyll build -d /app/_site/site/ --config _config.yml,/tmp/_config_additional.yml
      args:
        executable: /bin/bash
        chdir: /srv/jekyll-data/site/
    - shell: |
        cp -Rf /app/_site/site/assets /app/_site/documentation/
        cp -Rf /app/_site/site/images /app/_site/documentation/
        cp -Rf /app/_site/site/includes /app/_site/documentation/
        cp -Rf /app/_site/site/ru/includes /app/_site/documentation/ru
        cp -Rf /app/_site/site/en/includes /app/_site/documentation/en
        cp -Rf /app/_site/site/ru /app/_site/documentation/
        cp -Rf /app/_site/site/en /app/_site/documentation/
        rm -rf /app/_site/documentation/compare/
import:
  - artifact: yq4-artifact
    add: /usr/local/bin/yq
    to: /usr/local/bin/yq
    before: install
git:
- add: /{{ .ModulePath }}docs
  to: /srv/jekyll-data
  owner: jekyll
  group: jekyll
  includePaths:
    - documentation
    - site
  excludePaths:
    - '**/werf*.yaml'
    - '**/docker-compose.yml'
    - '**/Makefile'
    - '**/.werf'
    - '**/.helm'
    - documentation/pages/internal
    - documentation/config
    - site/pages/*.html
    - site/_includes/footer.html
    - site/_data/topnav.*
    - site/backends
  stageDependencies:
    install: ['site/Gemfile','site/Gemfile.lock']
    setup: '**/*'
- add: /{{ .ModulePath }}
  to: /comparison
  owner: jekyll
  group: jekyll
  stageDependencies:
    setup: ['**/*']
  includePaths:
  - modules/**/docs/README.md
  - ee/modules/**/docs/README.md
- add: /{{ .ModulePath }}ee/fe
  to: /comparison/fe
  owner: jekyll
  group: jekyll
  stageDependencies:
    setup: ['**/*']
  includePaths:
  - modules/**/docs/README.md
- add: /{{ .ModulePath }}ee/be
  to: /comparison/be
  owner: jekyll
  group: jekyll
  stageDependencies:
    setup: ['**/*']
  includePaths:
  - modules/**/docs/README.md
- add: /{{ .ModulePath }}ee/se
  to: /comparison/se
  owner: jekyll
  group: jekyll
  stageDependencies:
    setup: ['**/*']
  includePaths:
  - modules/**/docs/README.md
{{ tpl (.Files.Get (printf "%sdocs/documentation/werf-git-section.inc.yaml" .ModulePath)) . }}
{{ tpl (.Files.Get (printf "%sdocs/site/werf-docs-builder.inc.yaml" .ModulePath)) (dict "ctx" . "mode" "module") }}
